package com.relsell.view;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;

import com.relsell.admin.AboutUs;
import com.relsell.admin.Admin;
import com.relsell.pointofsales.MainActivity;
import com.relsell.pointofsales.R;
import com.relsell.utility.AppVars;
import com.relsell.utility.UtilitySingleTon;

/**
 * 
 * Manager Class that manages manager User Dashboard.
 * 
 */
public class Manager extends FragmentActivity {

	public static final int MENU_REGISTER = 1004;
	
	@Override
	public void onBackPressed() {
		
		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
				Manager.this);

		alertDialogBuilder.setTitle(R.string.exit_dialog_title);
		alertDialogBuilder
				.setMessage(R.string.exit_dialog_txt)
				.setCancelable(true)
				.setPositiveButton(R.string.exit_txt,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int id) {

								Manager.this.finish();
								Manager.super.onBackPressed();

							}
						})
				.setNegativeButton(R.string.switch_user_txt,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
								Intent intent = new Intent(Manager.this,
										Login.class);
								startActivity(intent);
								Manager.this.finish();
							}
						});

		AlertDialog alertDialog = alertDialogBuilder.create();
		alertDialog.show();

	}

	private TabHost mTabHost;
	private ViewPager mViewPager;
	private TabsAdapter mTabsAdapter;
	private View tabOne;
	private View tabTwo;
	private View tabBarOne;
	private View tabBarTwo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.fragmentadapter);
		mTabHost = (TabHost) findViewById(android.R.id.tabhost);
		mTabHost.setup();

		mViewPager = (ViewPager) findViewById(R.id.pager);
		mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager);

		LayoutInflater li = this.getLayoutInflater();

		tabTwo = li.inflate(R.layout.custom_tab, null);

		TextView t1 = (TextView) tabTwo.findViewById(R.id.tabsText);
		t1.setText("Reports");
		tabBarTwo = (View) tabTwo.findViewById(R.id.view1);
		tabBarTwo.setSelected(true);

		mTabsAdapter.addTab(mTabHost.newTabSpec("two").setIndicator(tabTwo),
				ManagerReports.class, null);
		
		
		tabOne = li.inflate(R.layout.custom_tab, null);
		TextView t = (TextView) tabOne.findViewById(R.id.tabsText);
		t.setText("Maps");
		tabBarOne = (View) tabOne.findViewById(R.id.view1);
		tabBarOne.setSelected(false);
		mTabsAdapter.addTab(mTabHost.newTabSpec("one").setIndicator(tabOne),
				Maps.class, null);

		

		if (savedInstanceState != null) {
			mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
		}
		UtilitySingleTon.getInstance().setTabsAdapter(mTabsAdapter);
		UtilitySingleTon.getInstance().setTabHost(mTabHost);
	}

	public static class TabsAdapter extends FragmentPagerAdapter implements
			TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
		private final Context mContext;
		private final TabHost mTabHost;
		private final ViewPager mViewPager;
		private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

		static final class TabInfo {
			private final String tag;
			private final Class<?> clss;
			private final Bundle args;

			TabInfo(String _tag, Class<?> _class, Bundle _args) {
				tag = _tag;
				clss = _class;
				args = _args;
			}
		}

		static class DummyTabFactory implements TabHost.TabContentFactory {
			private final Context mContext;

			public DummyTabFactory(Context context) {
				mContext = context;
			}

			@Override
			public View createTabContent(String tag) {
				View v = new View(mContext);
				v.setMinimumWidth(0);
				v.setMinimumHeight(0);
				return v;
			}
		}

		public TabsAdapter(FragmentActivity activity, TabHost tabHost,
				ViewPager pager) {
			super(activity.getSupportFragmentManager());
			mContext = activity;
			mTabHost = tabHost;
			mViewPager = pager;
			mTabHost.setOnTabChangedListener(this);
			mViewPager.setAdapter(this);
			mViewPager.setOnPageChangeListener(this);
		}

		public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
			tabSpec.setContent(new DummyTabFactory(mContext));
			String tag = tabSpec.getTag();

			TabInfo info = new TabInfo(tag, clss, args);
			mTabs.add(info);
			mTabHost.addTab(tabSpec);
			notifyDataSetChanged();
		}

		@Override
		public int getCount() {
			return mTabs.size();
		}

		@Override
		public Fragment getItem(int position) {
			TabInfo info = mTabs.get(position);

			return Fragment.instantiate(mContext, info.clss.getName(),
					info.args);

		}

		@Override
		public void onTabChanged(String tabId) {
			int position = mTabHost.getCurrentTab();
			mViewPager.setCurrentItem(position);
		}

		@Override
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {
		}

		@Override
		public void onPageSelected(int position) {
			// Unfortunately when TabHost changes the current tab, it kindly
			// also takes care of putting focus on it when not in touch mode.
			// The jerk.
			// This hack tries to prevent this from pulling focus out of our
			// ViewPager.
			TabWidget widget = mTabHost.getTabWidget();
			int oldFocusability = widget.getDescendantFocusability();
			widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
			widget.setDescendantFocusability(oldFocusability);
		}
		
		/*public int getItemPosition(Object object) {
		    return POSITION_NONE;
		}*/

		@Override
		public void onPageScrollStateChanged(int state) {
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu, menu);// Menu Resource, Menu
		menu.add(0,MENU_REGISTER,1,"Register");
		return true;

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {

		case R.id.menu_logout:
			AppVars.dbBackup(Manager.this);
			Intent intent = new Intent(Manager.this, Login.class);
			startActivity(intent);
			finish();
			return true;
		case MENU_REGISTER:
			this.setProgressBarIndeterminateVisibility(true);
			new Register().execute();
			this.setProgressBarIndeterminateVisibility(false);
			Toast.makeText(getApplicationContext(), "Successfully Registered",
					Toast.LENGTH_LONG).show();
			return true;

		case R.id.menu_aboutus:
			Intent in = new Intent(Manager.this, AboutUs.class);
			startActivity(in);
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}
	}

}
